let redis = require("redis");

class RedisClient {
    constructor(port, ip) {
        this.port = port;
        this.ip = ip;
    }
    connect(){
        this.client = redis.createClient(this.port, this.ip);
        this.client.on('ready', function(res){
            if (global.log) {
                global.log.print("redis ready");
            } else {
                console.log("redis ready");
            }
        });
        this.client.on('error', function(res){
            if (global.log) {
                global.log.error(res);
            } else {
                console.error(res);
            }
        });
    }
    expire(k, sec){
        let self = this;
        return new Promise(function (resolve, reject) {
            if (sec>0) {
                self.client.expire(k, sec, function(err){
                    if (err) {
                        reject(err);
                    } else {
                        resolve(true);
                    }
                });
            } else {
                reject("second error");
            }
        })
    }
    ttl(k){
        let self = this;
        return new Promise(function (resolve, reject) {
            self.client.ttl(k, function(err, num){
                if (err) {
                    reject(err);
                } else {
                    resolve(num);
                }
            });
        })
    }
    set(k,v){
        let self = this;
        return new Promise(function (resolve, reject) {
            self.client.set(k,v, function(err){
                if (err) {
                    reject(err);
                } else {
                    resolve(true);
                }
            });
        })
    }
    get(k,parsejson=false){
        let self = this;
        return new Promise(function (resolve, reject) {
            self.client.get(k, function(err, value){
                if (err) {
                    reject(err);
                } else {
                    if (parsejson) {
                        resolve(JSON.parse(value));
                    } else {
                        resolve(value);
                    }
                }
            });
        })
    }
    hset(k,f,v){
        let self = this;
        return new Promise(function (resolve, reject) {
            self.client.hset(k,f,v, function(err){
                if (err) {
                    reject(err);
                } else {
                    resolve(true);
                }
            });
        })
    }
    hmset(k,...args){
        let self = this;
        return new Promise(function (resolve, reject) {
            self.client.hmset(k,args, function(err){
                if (err) {
                    reject(err);
                } else {
                    resolve(true);
                }
            });
        })
    }
    hget(k,f,parsejson=false){
        let self = this;
        return new Promise(function (resolve, reject) {
            self.client.hget(k,f, function(err, value){
                if (err) {
                    reject(err);
                } else {
                    if (parsejson) {
                        resolve(JSON.parse(value));
                    } else {
                        resolve(value);
                    }
                }
            });
        })
    }
    hgetall(k){
        let self = this;
        return new Promise(function (resolve, reject) {
            self.client.hgetall(k, function(err, value){
                if (err) {
                    reject(err);
                } else {
                    resolve(value);
                }
            });
        })
    }
    incr(k){
        let self = this;
        return new Promise(function (resolve, reject) {
            self.client.incr(k, function(err, value){
                if (err) {
                    reject(err); 
                } else {
                    resolve(value);
                }
            });
        })
    }
    //如果没有过期则刷新过期时间
    refreshExpire(k, sec){
        let self = this;
        this.ttl(k).then(num=>{
            if (num > 0 && num < sec/2) {
                self.client.expire(k, sec);
                return true;
            }
        }).catch(err=>{
            if (global.log) {
                global.log.error(err);
            } else {
                console.error(err);
            }
        });
        return false;
    }
}

module.exports = RedisClient;